/*
 * Tencent is pleased to support the open source community by making TubeMQ available.
 *
 * Copyright (C) 2012-2019 Tencent. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 * this file except in compliance with the License. You may obtain a copy of the
 * License at
 *
 * https://opensource.org/licenses/Apache-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 */

package com.tencent.tubemq.server.master.nodemanage.nodeproducer;

import com.tencent.tubemq.corebase.cluster.ProducerInfo;

import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;


public class ProducerInfoHolder {

    final ConcurrentHashMap<String/* producerId */, ProducerInfo> producerInfoMap =
            new ConcurrentHashMap<String, ProducerInfo>();

    public ProducerInfo getProducerInfo(String producerId) {
        return producerInfoMap.get(producerId);
    }

    public void setProducerInfo(String producerId,
                                Set<String> topicSet,
                                String host, boolean overTLS) {
        producerInfoMap.put(producerId,
                new ProducerInfo(producerId, topicSet, host, overTLS));
    }

    public void updateProducerInfo(String producerId,
                                   Set<String> topicSet,
                                   String host,
                                   boolean overTLS) {
        ProducerInfo curProducer =
                producerInfoMap.get(producerId);
        ProducerInfo newProducer =
                new ProducerInfo(producerId, topicSet, host, overTLS);
        if (!newProducer.equals(curProducer)) {
            producerInfoMap.put(producerId, newProducer);
        }
    }

    public ProducerInfo removeProducer(String producerId) {
        return producerInfoMap.remove(producerId);
    }

    public void clear() {
        producerInfoMap.clear();
    }
}
